<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '菜单添加' : '菜单编辑'"
      :visible.sync="obj.isTrue"
      width="50%"
      @close="cancel"
    >
      <el-form ref="form" :model="form">
        <el-form-item label="菜单名称" >
          <el-input v-model="form.title"></el-input> </el-form-item
        >{{ form.title }}
        <el-form-item label="上级菜单">
          <el-select
            v-model="form.pid"
            placeholder="请选择上级菜单"
            @change="changePid()"
          >
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in menuList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option> </el-select
          >{{ form.pid }}
        </el-form-item>
        <el-form-item label="菜单类型">
          <el-radio :label="1" v-model="form.type" disabled>目录</el-radio>
          <el-radio :label="2" v-model="form.type" disabled
            >菜单</el-radio
          > </el-form-item
        >{{ form.type }}
        <el-form-item label="菜单图标" v-show="form.type == 1">
          <el-select v-model="form.icon" placeholder="请选择菜单图标">
            <el-option
              v-for="(item, index) in iconArr"
              :key="index"
              :value="item"
            >
              <i :class="item"></i>
            </el-option> </el-select
          >{{ form.icon }}
        </el-form-item>
        <el-form-item label="菜单地址" v-show="form.type == 2">
          <el-select v-model="form.url" placeholder="请选择菜单地址">
            <el-option
              v-for="(item, index) in secondRouter"
              :key="index"
              :label="item.meta.title"
              :value="'/' + item.path"
            ></el-option> </el-select
          >{{ form.url }}
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch> </el-form-item
        >{{ form.status }}
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button v-if="obj.isAdd" type="primary" @click="add()"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="edit()">编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { secondRouter } from "../../../router/index.js";
import { menuAdd, menuInfo, menuEdit } from "../../../request/api.js";
import { successAlert, warningAlert } from "../../../utils/alert.js";
import { mapActions, mapGetters } from "vuex";

export default {
  props: ["obj"],
  data() {
    return {
      form: {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      iconArr: [
        "el-icon-eleme",
        "el-icon-setting",
        "el-icon-goods",
        "el-icon-help",
        "el-icon-folder",
      ],
      secondRouter: secondRouter,
    };
  },
  methods: {
    ...mapActions({
      asyncMenuList: "menu/asyncChangeList",
    }),
    reset() {
      this.form = {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      };
    },
    changePid() {
      ///如果pid=0 说明选中的是顶级菜单 此时菜单类型为目录
      if (this.form.pid == 0) {
        this.form.type = 1;
        //并且清空上一次选中的地址
        this.form.url = "";
      } else {
        //否则选中的不是顶级菜单 此时菜单类型应为菜单
        this.form.type = 2;
        //并且清空上一次选中的图标
        this.form.icon = "";
      }
    },
    cancel() {
      //关闭对话框
      this.obj.isTrue = false;
      //清楚form表单的值
      this.reset();
    },
    add() {
      //调用接口 添加数据
      menuAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
        } else {
          warningAlert(res.data.msg);
        }
      });
      //重新请求列表数据
      this.asyncMenuList();
    },
    getinfo(id) {
      menuInfo({ id: id }).then((res) => {
        // 给form赋值，用来渲染页面
        this.form = res.data.list;
        // 给form添加id字段，因为未来我们在确定编辑的时候需要用到
        this.form.id = id;
      });
    },
    edit() {
      menuEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          //如果编辑成功 显示编辑成功 关闭对话框并情空form 重新请求列表数据
          successAlert(res.data.msg);
          this.cancel();
          this.asyncMenuList();
        } else {
          //失败的提示
          warningAlert(res.data.msg);
        }
      });
    },
  },
  computed: {
    ...mapGetters({
      menuList: "menu/list",
    }),
  },
  mounted() {},
};
</script>

<style>
</style>